package com.orharlan.carassistant.features;

import com.orharlan.carassistant.R;
import com.orharlan.carassistant.datamodel.Datamodel;
import com.orharlan.carassistant.gui.MainActivity;
import com.orharlan.carassistant.gui.SettingsGPSActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class SaveGpsBtListener implements LocationListener
{

	private LocationManager locationManager;
	private Context context;
	private boolean gpsActivity;
	private Datamodel data;

	public SaveGpsBtListener( Context context )
	{
		data = Datamodel.singleton( );
		this.context = context;

	}

	public void start( String mode )
	{

		/********** get Gps location service LocationManager object ***********/
		// Register the class as listener to retrieve a LocationManager for
		// controlling location updates.
		locationManager = (LocationManager) context.getSystemService( Context.LOCATION_SERVICE );

		/*
		 * Parameters : First(provider) : the name of the provider with which to
		 * register Second(minTime) : the minimum time interval for
		 * notifications, in milliseconds. This field is only used as a hint to
		 * conserve power, and actual time between location updates may be
		 * greater or lesser than this value. Third(minDistance) : the minimum
		 * distance interval for notifications, in meters Fourth(listener) : a
		 * {#link LocationListener} whose onLocationChanged(Location) method
		 * will be called for each location update
		 */

		locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 3000, // 3
		                                                                            // sec
		                                        0, this );

		// This provides the query the location by WLAN
		// locationManager.requestLocationUpdates(
		// LocationManager.NETWORK_PROVIDER, 3000,// 3
		// // sec
		// 10, this );
		/*********
		 * After registration onLocationChanged method called periodically after
		 * each 3 sec
		 ***********/

	}

	public void stop( )
	{
		if( locationManager != null )
		{
			locationManager.removeUpdates( this );
		}
	}

	/************* Called after each 3 sec **********/
	@Override
	public void onLocationChanged( Location location )
	{
		data.setCurrentLocation( location.getLatitude( ), location.getLongitude( ) );
	}

	@Override
	public void onProviderDisabled( String provider )
	{

		/******** Called when User off Gps *********/
		gpsActivity = false;
		promptAlertDialog( "GPS", context.getString( R.string.alertDialogNoGps ) );
		Toast.makeText( context, "Gps turned off", Toast.LENGTH_LONG ).show( );
	}

	@Override
	public void onProviderEnabled( String provider )
	{

		/******** Called when User turned on Gps *********/
		if( gpsActivity )
		{
			Intent intent = new Intent( context, SettingsGPSActivity.class );
			intent.putExtra( "mode", SettingsGPSActivity.modeFlag );
			context.startActivity( intent );
			Toast.makeText( context, "Gps turned on ", Toast.LENGTH_LONG ).show( );
			gpsActivity = false;
		}

	}

	@Override
	public void onStatusChanged( String provider, int status, Bundle extras )
	{
		// TODO Auto-generated method stub
	}

	public void promptAlertDialog( String alertTitle, String alertText )
	{
		if( context != null )
		{
			AlertDialog.Builder builder1 = new AlertDialog.Builder( context );
			builder1.setTitle( alertTitle );
			builder1.setMessage( alertText );
			builder1.setCancelable( false );
			builder1.setPositiveButton( context.getString( R.string.alertDialogYes ), new DialogInterface.OnClickListener( )
			{
				@Override
				public void onClick( DialogInterface dialog, int id )
				{
					gpsActivity = true;
					Intent intent = new Intent( android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS );
					context.startActivity( intent );
					dialog.cancel( );
				}
			} );
			builder1.setNegativeButton( context.getString( R.string.alertDialogNo ), new DialogInterface.OnClickListener( )
			{
				@Override
				public void onClick( DialogInterface dialog, int id )
				{
					MainActivity.showLongToast( context, context.getString( R.string.noGps ) );
					context.startActivity( new Intent( context, MainActivity.class ) );
					dialog.cancel( );
				}
			} );

			AlertDialog alert11 = builder1.create( );
			alert11.show( );
		}
	}

	public void setContext( Context context )
	{
		this.context = context;
	}
}
